package com.ecode.annotation.builder.proj;

import com.ecode.annotation.EcodeDo;
import com.ecode.annotation.ErrorCode;
import com.ecode.annotation.core.CodeDataPullBuilder;

import java.io.*;

/**
 * @ClassName ProjectCodeDataPullBuilder
 * @Author mjlft
 * @Date 2021/10/4 11:58
 * @Version 1.0
 * @Description 项目配置数据拉取构建器
 */
public class ProjectCodeDataPullBuilder implements CodeDataPullBuilder {

    @Override
    public String dataPull(EcodeDo ecodeDo) {
        String projectConfigPath = ecodeDo.getProjectConfigPath();
        if (projectConfigPath == null || "".equals(projectConfigPath.trim())) {
            throw new RuntimeException("注解缺少projectConfigPath配置");
        }
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        StringBuffer stringBuffer = new StringBuffer();
        try {
            String filePath = this.getClass().getClassLoader().getResource(projectConfigPath).getPath();
            inputStream = new FileInputStream(filePath);
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String tmpStr = null;
            while ((tmpStr = bufferedReader.readLine()) != null ){
                stringBuffer.append(tmpStr.trim() + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stringBuffer.toString();
    }
}
